package com.yezi.lampcourt.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yezi.lampcourt.R;
import com.yezi.lampcourt.base.BaseActivity;
import com.yezi.lampcourt.entity.CourtBean;
import com.yezi.lampcourt.ui.adapter.CourtListAdapter;
import com.yezi.lampcourt.ui.contract.CourtListContract;
import com.yezi.lampcourt.ui.presenter.CourtListPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by HongMing Ye
 * on 2017/9/19 0019.
 */

public class CourtListActivity extends BaseActivity<CourtListPresenter> implements CourtListContract.CourtListView, OnRefreshListener {


    @BindView(R.id.rv_court_list)
    RecyclerView mRvCourtList;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout mSrlRefresh;

    private CourtListAdapter mCourtListAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_court_list);
        ButterKnife.bind(this);
        init();
    }

    @Override
    protected void init() {
        mSrlRefresh.setOnRefreshListener(this);
        mRvCourtList.setLayoutManager(new LinearLayoutManager(this));
        mPresenter = new CourtListPresenter(this, this);
        mPresenter.getCourtList();
    }

    @Override
    public void getCourtListSuccess(final List<CourtBean> courtBeanList) {
        if (mCourtListAdapter == null) {
            mCourtListAdapter = new CourtListAdapter(R.layout.recycler_item_court, courtBeanList);
            mRvCourtList.setAdapter(mCourtListAdapter);
        } else {
            mCourtListAdapter.setNewData(courtBeanList);
        }
        mCourtListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(CourtListActivity.this, CourtActivity.class);
                intent.putExtra("courtBean", courtBeanList.get(position));
                startActivity(intent);
            }
        });
        mSrlRefresh.finishRefresh();
    }

    @Override
    public void getCourtListFail(String msg) {
        mSrlRefresh.finishRefresh();
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        mPresenter.getCourtList();
    }
}
